class KMailerController < ApplicationController
  
  before_filter :login_required
  
  def write_mail
    @recipient = (params[:recipient])? params[:recipient] : ''
    @cc = (params[:cc])? params[:cc] : ''
    @bcc = (params[:bcc])? params[:bcc] : ''
  end
  
  def send_mail
    raise ArgumentError, 'Falta informaci&oacute;n' unless params[:email]
    
    email = params[:email]

    @recipient = email[:recipient]
    @cc = email[:cc]
    @bcc = email[:bcc]
    @subject = email[:subject]
    @message = email[:message]
    
    raise ArgumentError, "A: e-mail invalido." if email[:recipient] != '' and not VALID_MAIL_REGEX.match(email[:recipient])
    raise ArgumentError, "Cc: e-mail invalido." if email[:cc] != '' and not VALID_MAIL_REGEX.match(email[:cc])
    raise ArgumentError, 'Bcc: e-mail invalido.' if email[:bcc] != '' and not VALID_MAIL_REGEX.match(email[:bcc])

    KMailer.deliver_plainmail(@recipient, @cc, @bcc, @subject, @message, email[:from])
    flash[:success] = "Mail enviado."
    redirect_back_or_default :action=>'write_mail'        
    
  rescue => e
    flash[:error] = e.message
    render :action=>'write_mail'
  end
  
  def mailing_lists
    store_location
    @tags = Persona.tag_counts
  end

  def mailing_to_tag
    if params[:tag] then
      tag = params[:tag]
      @list_name = "Etiquetados con '#{tag}'"
      mails = Persona.find_tagged_with(tag).collect{ |p| p.mail }
      mails.delete_if{|m| m == ''}
      build_list mails
      render :action=>'write_to_list'
    else
      redirect_back_or_default :action=>'mailing_lists'
    end
  end

  def mailing_alumnos_instr
    @instr = (params[:id])? Instructor.find(params[:id]) : current_user.instructor
    @list_name = "Alumnos de #{@instr.nombre}"
    build_list @instr.alumnos.find(:all, :conditions => "mail <> ''").collect { |p| p.mail }
    render :action=>'write_to_list'
  end
  
  def mailing_externos
    @list_name = "Todos los externos (fps, p, exalumnos)"
    build_list Persona.find_not_alumnos(:all, :conditions => "mail <> ''").collect { |p| p.mail }
    render :action=>'write_to_list'
  end
  
  def mailing_pmas
    @list_name = "P+"
    build_list Persona.find(:all, :conditions => "coeficiente_id=#{Coeficiente::PMAS} AND mail <> ''").collect { |p| p.mail }
    render :action=>'write_to_list'    
  end
  
  def mailing_prospects
    @list_name = "Prospects"
    build_list Persona.find_prospects(:all, :conditions => "mail <> ''").collect { |p| p.mail }
    render :action=>'write_to_list'
  end

  def mailing_fps
    @list_name = "FPs"
    build_list Persona.find_fps(:all, :conditions => "mail <> ''").collect { |p| p.mail }
    render :action=>'write_to_list'
  end
  
  def mailing_preyoga
    @list_name = "Alumnos de Pre-Y&ocirc;ga"
    build_list Persona.find_alumnos(:all, :conditions => "mail <> '' and grado_id=#{Grado::ASPIRANTE}").collect { |p| p.mail }
    render :action=>'write_to_list'
  end
  
  def mailing_yoga
    @list_name = "Alumnos de Y&ocirc;ga"
    build_list Persona.find_alumnos(:all, :conditions => "mail <> '' and  grado_id>#{Grado::ASPIRANTE}").collect { |p| p.mail }
    render :action=>'write_to_list'
  end
  
  def mailing_alumnos
    @list_name = "Todos los Alumnos"
    build_list Persona.find_alumnos(:all, :conditions => "mail <> ''").collect { |p| p.mail }
    render :action=>'write_to_list'
  end
  
  def mailing_exalumnos
    @list_name = "Exalumnos"
    build_list Persona.find_ex_alumnos(:all, :conditions => "mail <> ''").collect{|p| p.mail}
    render :action=>'write_to_list'
  end

  def mailing_visitante_perfil_del_mes
    fecha_mes = (params[:fecha])? Date.parse(params[:fecha]) : Date.today
    @list_name = "Visitantes perfil de #{fecha_mes.strftime("%b-%y")}"
    build_list Visita.find_perfil(:all, :conditions=>"MONTH(fecha)=#{fecha_mes.month} AND YEAR(fecha)=#{fecha_mes.year}").collect{|v| v.persona.mail if v.persona.mail!=''}
    render :action=>'write_to_list'
  end
  
  def write_to_list
  end
  
  def send_to_list
    raise ArgumentError, 'Falta informaci&oacute;n.' unless params['email']
    email = params["email"]
    
    @mail_list = email["bcc"]
    @from = email['from']
    @subject = email["subject"]
    @message = email["message"]
    KMailer.deliver_plainmail(nil, nil, @mail_list, @subject, @message, @from)
    redirect_back_or_default :action=>'write_to_list'
    
  rescue => e
    flash[:error] = e.message
    render :action=>'write_mail'
  end

  private
  def build_list(mails)
    mails.delete_if{ |mail| not VALID_MAIL_REGEX.match(mail) }
    @cant_mails = mails.size
    @mail_list = (mails and mails.size)? mails.join('; ') : ' '
  end
  
end
